var oForm = document.querySelector("#formData");
var aInput = document.querySelectorAll("#formData input");
var addSaveBtn = document.querySelector(".addSaveBtn");
// console.log(oForm);
// console.log(aInput);
// console.log(addSaveBtn);
// 定义正则
var rule = {
  addId: {
    reg: /^[1-9]{1,4}$/,
    title: "格式错误,编号为：(1-4)位数纯数字！",
  },
  addName: {
    reg: /^[\u4e00-\u9fa5]{2,6}$/,
    title: "请输入中文的姓名",
  },
};
for (var i = 0; i < aInput.length; i++) {
  aInput[i].onblur = function () {
    var str = this.value;
    console.log(str);
    if (str.trim() == "") {
      this.nextElementSibling.innerHTML = "格式错误！";
      this.nextElementSibling.style.color = "red";
    } else if (rule[this.name].reg.test(str) == false) {
      this.nextElementSibling.innerHTML = rule[this.name].title;
      this.nextElementSibling.style.color = "red";
      console.log(rule[this.name].reg.test(str));
    } else {
      this.nextElementSibling.innerHTML = "√";
      this.nextElementSibling.style.color = "green";
    }
  };
}